package com.example.thirdhomework

import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle

class MainActivity5 : AppCompatActivity() {
    private var seconds = 0
    private var running = false
    private var wasRunning = false
    private lateinit var textViewTime: TextView
    private lateinit var buttonStart: Button
    private lateinit var buttonStop: Button
    private lateinit var buttonReset: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main5)

        Log.d("Life Cycle", "onCreate")

        if (savedInstanceState != null) {
            seconds = savedInstanceState.getInt("seconds")
            running = savedInstanceState.getBoolean("running")
            wasRunning = savedInstanceState.getBoolean("wasRunning")
        }

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        textViewTime = findViewById(R.id.textView_timer)
        buttonStart = findViewById(R.id.button_start)
        buttonStop = findViewById(R.id.button_stop)
        buttonReset = findViewById(R.id.button_restart)
        runTimer()

        buttonStart.setOnClickListener {
            running = true
        }

        buttonStop.setOnClickListener {
            running = false
        }

        buttonReset.setOnClickListener {
            running = false
            seconds = 0
        }
    }

    private fun runTimer() {
        val handler = Handler()
        val runnable = object : Runnable {
            override fun run() {
                val hours = seconds / 3600
                val minutes = seconds % 3600 / 60
                val secs = seconds % 60
                textViewTime.text = String.format("%d:%02d:%02d", hours, minutes, secs)
                if (running) {
                    seconds++
                }
                handler.postDelayed(this, 1000)
            }
        }
        handler.post(runnable)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d("Life Cycle", "onSaveInstanceState")
        outState.putInt("seconds", seconds)
        outState.putBoolean("running", running)
        outState.putBoolean("wasRunning", wasRunning)
    }

    //lfc,life cycle,使计时功能在程序在后台时停止
    override fun onPause() {
        super.onPause()
        Log.d("Life Cycle", "onPause")
        wasRunning = running
        running = false
    }

    override fun onResume() {
        super.onResume()
        Log.d("Life Cycle", "onResume")
        if (wasRunning) {
            running = true
        }
    }

    override fun onStart() {
        super.onStart()
        Log.d("Life Cycle", "onStart")
    }

    override fun onStop() {
        super.onStop()
        Log.d("Life Cycle", "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("Life Cycle", "onDestroy")
    }



}